package jh.jianguan.springboot.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jh.jianguan.springboot.dao.UserMapper;
import jh.jianguan.springboot.dto.common.security.SecurityUserDetails;
import jh.jianguan.springboot.model.User;
import jh.jianguan.springboot.service.IUserService;
import jh.jianguan.springboot.util.RedisHashUtil;
import jh.jianguan.springboot.util.common.exception.ExceptionUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 方法描述
 * @return
 * @author guoc
 * @date 2020/11/5 15:58
 */
@Service
@Slf4j
public class UserService implements IUserService {

    @Autowired
    UserMapper mapper;

    @Autowired
    RedisHashUtil redisHashUtil;

    @Override
    public User getUser(int id){
        return mapper.Sel(id);
    }

    @Override
    public List<User> getAllUser(){
        return  mapper.getAllOrderPresent();
    }

    @Override
    public PageInfo<User> getAllUserForPage(int pageNo, int pageSize) {
        PageHelper.startPage(pageNo,pageSize);
        List<User> userList = mapper.getAllOrderPresent();
        PageInfo<User> userPageInfo = new PageInfo<>(userList);
        return userPageInfo;
    }

    @Override
    public User getUserAddcache(int id) {

        Object user = redisHashUtil.getMapVal("userInfo", id + "");
        if (user!=null){
            log.info((User) user+" 数据在缓存中读到");
            return (User) user;
        }else {
            User userDb = mapper.Sel(id);
            redisHashUtil.addMapOne("userInfo",id+"",userDb);
            redisHashUtil.expire("userInfo",10);
            log.info(userDb+"从数据库里拿到并存入redis缓存中");
            return userDb;
        }
    }

    @Override
    public String getExceptionMsg(String id) {
        if (id.equals("100")){
            throw ExceptionUtil.mxe("唐紅の恋歌");
        }
        return null;
    }
}
